package com.cn.web.controller.PartsManagement;

import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.PartsManagement.BusinessTypeTable;
import com.cn.bean.vo.PartsManagement.ComponentItemClassification;
import com.cn.config.config.Result;
import com.cn.service.server.PartsManagement.BusinessTypeService;
import com.cn.service.server.PartsManagement.ComponentItemClassificationService;
import com.cn.service.server.admin.UserDepartmentsServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/Comp")
@Transactional
public class ComponentItemClassificationController {
    private final ComponentItemClassificationService componentItemClassificationService;

    @Autowired(required = false)
    public ComponentItemClassificationController(ComponentItemClassificationService componentItemClassificationService) {
        this.componentItemClassificationService = componentItemClassificationService;
    }

    @RequestMapping("/select/ComponentItem/Classification/Aside")
    @ResponseBody
    public List<ComponentItemClassification> selectComponentItemClassificationAside() {
        List<ComponentItemClassification> componentItemClassifications = componentItemClassificationService.selectComponentItemClassificationAside();
        Map<Integer, ComponentItemClassification> nodeMap = new HashMap<>();

        // Step 1: 构建映射
        for (ComponentItemClassification nodeData : componentItemClassifications) {
            ComponentItemClassification node = new ComponentItemClassification();
            node.setMCode(nodeData.getMCode());
            node.setMId(nodeData.getMId());
            node.setMName(nodeData.getMName());
            node.setMDescription(nodeData.getMDescription());
            node.setIsLeaf(nodeData.getIsLeaf());
            node.setLevel(nodeData.getLevel());
            node.setLabel(nodeData.getMName());
            node.setParentId(nodeData.getParentId());
            node.setMStatus(nodeData.getMStatus());
            nodeMap.put(node.getMId(), node);
        }

        // Step 2: 构建树状结构
        for (ComponentItemClassification nodeData : componentItemClassifications) {
            ComponentItemClassification node = nodeMap.get(nodeData.getMId());
            if (node.getParentId() != 0) {
                ComponentItemClassification parent = nodeMap.get(node.getParentId());
                if (parent != null) {
                    parent.getChildren().add(node);
                }
            }
        }

        // Step 3: 找到根节点并返回
        List<ComponentItemClassification> roots = new ArrayList<>();
        for (ComponentItemClassification node : nodeMap.values()) {
            if (node.getParentId() == 0) {
                roots.add(node);
            }
        }

        return roots;
    }

    @RequestMapping("/select/ComponentItem/Classification")
    @ResponseBody
    public List<ComponentItemClassification> selectComponentItemClassification(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String cCodeInput = jsonObject.getString("cCodeInput");
        String cCnameInput = jsonObject.getString("cCnameInput");
        cCodeInput = (cCodeInput != null) ? cCodeInput : "";
        cCnameInput = (cCnameInput != null) ? cCnameInput : "";
        return componentItemClassificationService.selectComponentItemClassification(limit, page, cCodeInput, cCnameInput);
    }

    @RequestMapping("/select/ComponentItem/Classification/Text")
    @ResponseBody
    public List<ComponentItemClassification> selectComponentItemClassificationText(@RequestBody JSONObject jsonObject) {
        String cCodeInput = jsonObject.getString("cCodeInput");
        String cCnameInput = jsonObject.getString("cCnameInput");
        cCodeInput = (cCodeInput != null) ? cCodeInput : "";
        cCnameInput = (cCnameInput != null) ? cCnameInput : "";
        return componentItemClassificationService.selectComponentItemClassificationText(cCodeInput, cCnameInput);
    }

    @RequestMapping("/select/ComponentItem/Classification/Text/Aside")
    @ResponseBody
    public List<ComponentItemClassification> selectComponentItemClassificationTextAside(@RequestBody JSONObject jsonObject) {
        Integer parentID = jsonObject.getInteger("parentID");
        List<ComponentItemClassification> componentItemClassifications = componentItemClassificationService.selectComponentItemClassificationAside();
        List<ComponentItemClassification> result = new ArrayList<>();
        for (ComponentItemClassification type : componentItemClassifications) {
            if (type.getMId() == parentID) {
                result.add(type);
            }
            if (type.getParentId() == parentID) {
                // 找到与指定parentID匹配的根节点，调用递归函数获取其所有子节点
                result.add(type);
                addChildNodes(type, componentItemClassifications, result);
            }
        }
        System.out.println(result);
        //result  所有分类
        List<ComponentItemClassification> result1 = new ArrayList<>();
        List<ComponentItemClassification> com = componentItemClassificationService.selectChild();
        for (ComponentItemClassification componentItemClassification : com) {
            for (ComponentItemClassification a : result) {
                System.out.println(componentItemClassification.getClassificationId());
                System.out.println(a.getMId());
                if (componentItemClassification.getClassificationId() == a.getMId()) {
                    ComponentItemClassification modifiedComponentItemClassification = new ComponentItemClassification();
                    modifiedComponentItemClassification.setCId(componentItemClassification.getCId());
                    modifiedComponentItemClassification.setCCode(componentItemClassification.getCCode());
                    modifiedComponentItemClassification.setCName(componentItemClassification.getCName());
                    modifiedComponentItemClassification.setCDescription(componentItemClassification.getCDescription());
                    modifiedComponentItemClassification.setCStatus(componentItemClassification.getCStatus());
                    modifiedComponentItemClassification.setClassificationId(componentItemClassification.getClassificationId());
                    modifiedComponentItemClassification.setMId(a.getMId());
                    modifiedComponentItemClassification.setMCode(a.getMCode());
                    modifiedComponentItemClassification.setMName(a.getMName());
                    modifiedComponentItemClassification.setParentId(a.getParentId());
                    // 找到与指定parentID匹配的根节点，调用递归函数获取其所有子节点
//                    result1.add(componentItemClassification);
                    result1.add(modifiedComponentItemClassification);
                }
            }
        }
        return result1;
    }
    //查询状态为1的零件信息
    @RequestMapping("/select/Child/Auto1")
    @ResponseBody
    public List<ComponentItemClassification> selectChildAuto1(@RequestBody JSONObject jsonObject) {
        String cName = jsonObject.getString("input1");
        cName = (cName != null) ? cName : "";
        return componentItemClassificationService.selectChildAuto1(cName);
    }


    private void addChildNodes(ComponentItemClassification parent, List<ComponentItemClassification> allNodes, List<ComponentItemClassification> result) {
        for (ComponentItemClassification node : allNodes) {
            if (node.getParentId() == parent.getMId()) {
                // 如果当前节点是指定parentID的子节点，将其添加到结果列表中
                result.add(node);
                // 继续递归查找当前节点的子节点
                addChildNodes(node, allNodes, result);
            }
        }
        if (!result.contains(parent)) {
            result.add(parent);
        }
    }

    @RequestMapping("/add/ComponentItem/Classification/Aside")
    @ResponseBody
    public Result addComponentItemClassificationAside(@RequestBody JSONObject jsonObject) {
        String mName = jsonObject.getString("mName");
        String mDescription = jsonObject.getString("mDescription");
        Integer mStatus = jsonObject.getInteger("mStatus");
        Integer parentId = jsonObject.getInteger("parentID");
        Integer level = jsonObject.getInteger("level");
        if (parentId == null) {
            parentId = 1;
            level = 1;
        }
        return componentItemClassificationService.addComponentItemClassificationAside(mName, mDescription, mStatus, parentId, level, 0);
    }

    @RequestMapping("/add/ComponentItem/BasicInfoTable/")
    @ResponseBody
    public Result addComponentItemBasicInfoTable(@RequestBody JSONObject jsonObject) {
        String cName = jsonObject.getString("cName");
        String cDescription = jsonObject.getString("cDescription");
        Integer cStatus = jsonObject.getInteger("cStatus");
        Integer classificationId = jsonObject.getInteger("parentID");
        if (classificationId == null) {
            return Result.failure("500", "零件项目分类不可为空");
        }
        if (classificationId == 0) {
            return Result.failure("500", "零件项目分类不可直接选择");
        }
        return componentItemClassificationService.addComponentItemBasicInfoTable(cName, cDescription, cStatus, classificationId);
    }

    @RequestMapping("/select/Aside")
    @ResponseBody
    public List<ComponentItemClassification> selectAside(@RequestBody JSONObject jsonObject) {

        return componentItemClassificationService.selectAside();
    }

    @RequestMapping("/delete/Aside")
    @ResponseBody
    public Result deleteAside(@RequestBody JSONObject jsonObject) {
        Integer mId = jsonObject.getInteger("mId");
        return componentItemClassificationService.deleteAside(mId);
    }

    @RequestMapping("/delete/ComponentItem/BasicInfo")
    @ResponseBody
    public Result deleteComponentItemBasicInfo(@RequestBody JSONObject jsonObject) {
        Integer cId = jsonObject.getInteger("cId");
        return componentItemClassificationService.deleteComponentItemBasicInfo(cId);
    }

    @RequestMapping("/update/Aside")
    @ResponseBody
    public Result updateAside(@RequestBody JSONObject jsonObject) {
        Integer mId = jsonObject.getInteger("mId");
        String mName = jsonObject.getString("mName");
        String mDescription = jsonObject.getString("mDescription");
        Integer mStatus = jsonObject.getInteger("mStatus");
        Integer parentId = jsonObject.getInteger("parentID");
        Integer level = jsonObject.getInteger("level");
        if (parentId == null) {
            parentId = 1;
            level = 1;
        }
        return componentItemClassificationService.updateAside(mId, mName, mDescription, mStatus, parentId, level);
    }

    @RequestMapping("/update/ComponentItem/BasicInfo")
    @ResponseBody
    public Result updateComponentItemBasicInfo(@RequestBody JSONObject jsonObject) {
        Integer cId = jsonObject.getInteger("cId");
        String cName = jsonObject.getString("cName");
        String cDescription = jsonObject.getString("cDescription");
        Integer cStatus = jsonObject.getInteger("cStatus");
        Integer classificationId = jsonObject.getInteger("parentID");
        if (classificationId == null) {
            classificationId = 1;
        }
        return componentItemClassificationService.updateComponentItemBasicInfo(cId, cName, cDescription, cStatus, classificationId);
    }

}
